<?php
/**
 * Created by PhpStorm.
 * Script Name: Api.php
 * Create: 2020/4/14 15:46
 * Description: 与微信服务器交互事件
 * Author: fudaoji<fdj@kuryun.cn>
 */
namespace plugin\wechat\app\platform\controller;

use EasyOpen\Tencent\Constant\Mp\OpenEvent;
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
use plugin\wechat\app\platform\handler\mp\TextMessageHandler;
use plugin\wechat\app\platform\handler\mp\EventMessageHandler;
use plugin\wechat\app\platform\handler\mp\SpecialMessageHandler;
use plugin\wechat\app\platform\handler\platform\EventAuthorizedHandler;
use plugin\wechat\app\platform\handler\platform\EventThirdFastRegisteredBetaAppHandler;
use plugin\wechat\app\platform\handler\platform\EventUnAuthorizedHandler;
use plugin\wechat\app\platform\handler\platform\EventUpdateAuthorizedHandler;
use plugin\wechat\app\platform\handler\platform\EventThirdFastRegisteredHandler;
use plugin\wechat\app\platform\handler\platform\ReleaseMessageHandler;
use app\PluginController;
use EasyWeChat\OpenPlatform\Server\Guard;
use EasyWeChat\Kernel\Messages\Message;
use plugin\wechat\app\service\OpenPlatform;
use Support\Request;

class Api extends PluginController
{
    /**
     * 公众号消息与事件接收
     * @param Request $request
     * @throws InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\BadRequestException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \ReflectionException
     * @return false|string
     * @author fudaoji<fdj@kuryun.cn>
     */
    public function index(Request $request) {
        if(in_array($request->appId, OpenPlatform::testApps())) {
            //全网发布
            return $this->release();
        }

        /**
         * @var \EasyWeChat\OfficialAccount\Application|\EasyWeChat\MiniProgram\Application $app
         */
        $app = $request->mpApp;
        if(empty($request->mpInfo['status'])){ //禁用的公众号不做任何动作
            return $app->server->serve()->getContent();
        }

        $app->server->push(TextMessageHandler::class, Message::TEXT);
        $app->server->push(EventMessageHandler::class, Message::EVENT);
        $app->server->push(SpecialMessageHandler::class, Message::IMAGE|Message::LOCATION|Message::VOICE|Message::VIDEO|Message::LINK);
        //其他事件可按此依次注册事件处理器...
        return $app->server->serve()->getContent();
    }

    /**
     * 开放平台第三方平台推送事件处理
     * @param Request $request
     * @return
     * @author fudaoji<fdj@kuryun.cn>
     */
    public function ticket(Request $request) {
        $request->openPlatform->server->push(EventAuthorizedHandler::class, Guard::EVENT_AUTHORIZED);
        $request->openPlatform->server->push(EventUpdateAuthorizedHandler::class, Guard::EVENT_UPDATE_AUTHORIZED);
        $request->openPlatform->server->push(EventUnAuthorizedHandler::class, Guard::EVENT_UNAUTHORIZED);
        $request->openPlatform->server->push(EventThirdFastRegisteredHandler::class, Guard::EVENT_THIRD_FAST_REGISTERED);
        $request->openPlatform->server->push(EventThirdFastRegisteredBetaAppHandler::class, OpenEvent::EVENT_THIRD_FAST_REGISTERED_BETAAPP);
        
        //dao_log()->error(json_encode($request->all(), JSON_UNESCAPED_UNICODE));
        //var_dump(json_encode($request->all(), JSON_UNESCAPED_UNICODE));
        return $request->openPlatform->server->serve()->getContent();
    }

    /**
     * 全网发布
     * @author fudaoji<fdj@kuryun.cn>
     */
    private function release() {
        $request = \request();
        $request->mpApp->server->push(ReleaseMessageHandler::class, Message::TEXT|Message::EVENT);
        return $request->mpApp->server->serve()->getContent();
    }
}
